<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多线程</title>
</head>
<body>
    <button>点我-在主线程执行运算</button>
    <button>假死</button>
    <button>点我-在分线程执行运算</button>
</body>
<script>
    const btns = document.querySelectorAll("button");
    btns[0].onclick = function () {
        let startTime = Date.now();
        let sum = 0;
        for (let i = 0; i < 999999999; i++) {
            sum += Math.random();
        }
        console.log("计算结束：", sum)
        setTimeout(() => {
            console.log("over", Date.now() - startTime);
        }, 1000)
    }
    btns[1].onclick = function () {
        console.log("我执行啦！")
    }
    btns[2].onclick = function(){
        if(window.Worker){
            // console.log("支持")
            const worker = new Worker("./lib/my.js");
            worker.postMessage(99999999);
        }else{
            alert("浏览器不支持Worker");
        }
    }
</script>
</html>